<?php
include_once dirname(__FILE__)."/../../core/constants.php";
include_once dirname(__FILE__)."/../../core/mainpage.php";
include_once dirname(__FILE__)."/../../core/races/race_table.php";
include_once dirname(__FILE__)."/../../core/safe.php";
include_once dirname(__FILE__)."/../../core/game_management/users.php";

class RegisterPage extends MainPage
{
    function RegisterPage()
    {
        $strBlock = "";
        
        if(isset($_POST["tbxUserName"]) && isset($_POST["tbxNickName"]) && isset($_POST["tbxPassword"]) && isset($_POST["tbxMail"]) && isset($_POST["chbxAgreement"]))
        {   
            $userName = safePost("tbxUserName");
            $nickName = safePost("tbxNickName");
            $password = safePost("tbxPassword");
            $mail = safePost("tbxMail");
            
            if($userName == $nickName)
            {
                $strBlock = $this->showRegisterPage("ATTENZIONE: NickName ed UserName <u>non</u> devono coincidere!");
            }
            else if(strlen($userName) && strlen($nickName) && strlen($password) && strlen($mail))
            {
                if(User::exists($userName, $nickName, $mail))
                {
                    $strBlock = $this->showRegisterPage("Utente gia' registrato!");
                }
                else
                {
                    $u = new User();
                    $u->register($userName, $nickName, $password, $mail, $_POST["cbxRace"]);
                
                    $strBlock = $this->registrationSuccessful();
                }
            }
            else
            {
                $strBlock = $this->showRegisterPage("ATTENZIONE: Compila tutti i campi!");
            }
        }
        else
        {
            $strBlock = $this->showRegisterPage("");
        }
        
        parent::MainPage("Registrazione", $strBlock);
    }
    
    private function showRegisterPage($errMsg)
    {
        $pageText = REGISTER_TEXT;
        
        if(strlen($errMsg))
        {
            $pageText.= "<br/><br/><b>$errMsg</b>".
                        "<br/><br/>Inserisci i dati:<br/>";
        }
        else
        {
            $pageText.= "<br/><br/>Inserisci i dati:<br/>";            
        }
        
        $races = array();
        $plRaces = RaceTable::playableRaces();
        
        foreach($plRaces as $id => $name)
        {
            $races[$id] = new HTMLInputOption($id, $name);
        }

        $tbxUserName = new HTMLInput("text", "tbxUserName", "");
        $tbxNickName = new HTMLInput("text", "tbxNickName", "");
        $tbxPassword = new HTMLInputPassword("tbxPassword");
        $tbxMail = new HTMLInput("text", "tbxMail", "");
        $cbxRace = new HTMLInputDropDown("cbxRace", $races);
        $chbxAgreement = new HTMLInputCheckbox("chbxAgreement", "1");

        $cell1 = new HTMLTableCell("UserName:");
        $cell1->setAttribute("align", "right");
        $cell2 = new HTMLTableCell($tbxUserName);
        $row1 = new HTMLTableRow(array($cell1, $cell2));
        
        $cell1 = new HTMLTableCell("NickName:");
        $cell1->setAttribute("align", "right");
        $cell2 = new HTMLTableCell($tbxNickName);
        $row2 = new HTMLTableRow(array($cell1, $cell2));

        $cell1 = new HTMLTableCell("Password:");
        $cell1->setAttribute("align", "right");
        $cell2 = new HTMLTableCell($tbxPassword);
        $row3 = new HTMLTableRow(array($cell1, $cell2));

        $cell1 = new HTMLTableCell("Mail:");
        $cell1->setAttribute("align", "right");
        $cell2 = new HTMLTableCell($tbxMail);
        $row4 = new HTMLTableRow(array($cell1, $cell2));

        $cell1 = new HTMLTableCell("Razza:");
        $cell1->setAttribute("align", "right");
        $cell2 = new HTMLTableCell($cbxRace);
        $row5 = new HTMLTableRow(array($cell1, $cell2));
        
        $regTable = new HTMLTable(array($row1, $row2, $row3, $row4, $row5));
        $btnRegister = new HTMLInputSubmitButton("Registrati", "btnRegister");

        $agreement = $chbxAgreement->render()." Dichiaro di aver letto e accettato le condizioni di utilizzo.";
        $note = "<b>Nota</b>: UserName e' il nome usato per fare il login, 'NickName' e' il nome che avrai nel gioco.";
        $frm = new HTMLForm($regTable->render()."$agreement<br/><br/>$note<br/><br/>".$btnRegister->render(), createUrl("pages/registrati/index.php"), "POST");
        $pageText .= $frm->render();

        $block = new HTMLBlock($pageText);
        $block->setAttribute("align", "left");
        
        return $block->render();
    }
    
    private function registrationSuccessful()
    {
        $msg = styledText("#D2D2D2", "14", "La registrazione e' stata completata con successo!").
                          "A breve riceverai una mail contenente il link di attivazione, il link sara' valido
                           per 24h, dopo sara' necessario ripetere la registrazione.";
        
        $block = new HTMLBlock($msg);
        $block->setAttribute("align", "center");
        
        return $block->render();
    }
}

$p = new RegisterPage();
$ws = new WebPage("Registrazione", $p);
echo $ws->render();
?>
